﻿using ManagementApi.Models;
using ManagementApi.Mvc;
using ManagementApi.Mvc.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules;
using AutoMapper;
using PmSoft.Utilities;
using Modules.Logs;
using ManagementApi.Mvc.ApplicationContexts;

namespace ManagementApi.Controllers.AdminPannel
{
    /// <summary>
    /// 各应用log4net信息日志
    /// </summary>
    [Route("/api/v1/sysuser/log4netInfo")]
    [ApiController]
    [Authorize(Roles = "SysUser")]
    public class Log4netInfoController : ControllerBase
    {
        private readonly ManagementApplicationContext applicationContext;
        private readonly IMapper mapper;
        private readonly Log4netInfoService log4netInfoService;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="applicationContext">上下文</param>
        /// <param name="mapper">mapper映射</param>
        /// <param name="Log4netInfoService">主业务service</param>
        public Log4netInfoController(ManagementApplicationContext applicationContext,IMapper mapper,Log4netInfoService log4netInfoService)
        {
            this.applicationContext=applicationContext;
            this.mapper=mapper;
            this.log4netInfoService=log4netInfoService;
        }

        /***进入模版管理可以修改模版***/


        /// <summary>
        /// 获取各应用log4net信息日志列表(分页)
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<Log4netInfoDto>> GetLog4netInfos([FromQuery] Log4netQuery query, int pageNum = 1, int pageSize = 20)
        {
            var data = log4netInfoService.GetLog4netInfos(query, pageNum, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsDto(); });
            return ApiResult<PageDto<Log4netInfoDto>>.Success(dtos);
        }

        /// <summary>
        /// 创建各应用log4net信息日志信息
        /// </summary>
        /// <param name="form">表单数据</param>
        /// <returns></returns>
        [HttpPost("create")]
        [PermissionCheck("log4netInfo:add")]
        public ApiResult Add([FromBody] Log4netInfoEditForm form)
        {
            Log4netInfo entity = new ();
            mapper.Map(form, entity);
            entity.Date = DateTime.Now;
            log4netInfoService.Create(entity);
            return ApiResult.Success("创建成功");
        }

        /// <summary>
        /// 各应用log4net信息日志信息表单数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet("{id}/form")]
        [PermissionCheck("log4netInfo:details")]
        public ApiResult<Log4netInfoEditForm> GetRole(int id)
        {
            var data = log4netInfoService.Get(id);
            if (data == null)
            return ApiResult<Log4netInfoEditForm>.Failure("各应用log4net信息日志信息数据不存在");
            return ApiResult<Log4netInfoEditForm>.Success(data.AsForm());
        }

        /// <summary>
        /// 修改各应用log4net信息日志信息
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="form">表单信息</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [PermissionCheck("log4netInfo:edit")]
        public ApiResult Update(int id, [FromBody] Log4netInfoEditForm form)
        {
            var data = log4netInfoService.Get(id);
            if (data == null) return ApiResult.Failure("各应用log4net信息日志信息不存在");
            mapper.Map(form, data);
            log4netInfoService.Update(data);
            return ApiResult.Success("修改成功");
        }

        /// <summary>
        /// 删除各应用log4net信息日志信息
        /// </summary>
        /// <param name="ids">id集合，多个用,隔开</param>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        [PermissionCheck("log4netInfo:delete")]
        public ApiResult Delete(string ids)
        {
            log4netInfoService.Deleted(ValueUtility.ConvertToList<int>(ids));
            return ApiResult.Success("删除成功");
        }
    }
}
